//
//  LKSheetOptionViewController.m
//  SheetOptionViewController
//
//  Created by LIUKUN-PC on 16/5/4.
//  Copyright © 2016年 liukun. All rights reserved.
//

#import "LKOptionSheetViewController.h"

@interface LKOptionSheetViewController () <UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, copy) void(^didSelectOptionHandler)(NSInteger index);

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet UIButton *cancleButton;
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *tableViewHeightConstraint;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *cancelButtonBottomConstraint;
@property (weak, nonatomic) IBOutlet UIView *linerView;
@property (weak, nonatomic) IBOutlet UIView *customTitleView;

@property (nonatomic, assign) BOOL isHasTitle;

@end

@implementation LKOptionSheetViewController

static NSString * const cellResuedId = @"OptionSheetCell";

#pragma mark - Public API

- (void)didSelectOptionHandler:(void (^)(NSInteger))didSelectOptionBlock {
    self.didSelectOptionHandler = didSelectOptionBlock;
}

- (instancetype)initWithOptionsArray:(NSArray<NSString *> *)optionsArray didSelectOptionHandler:(void (^)(NSInteger))didSelectOptionBlock {
    if (self = [super initWithNibName:@"LKOptionSheetViewController" bundle:nil]) {
        self.modalPresentationStyle = UIModalPresentationOverCurrentContext;
        self.isDelectOptionRed = YES;
        self.redIndex = -1;
        _optionsArray = optionsArray;
        _didSelectOptionHandler = didSelectOptionBlock;
    }
    return self;
}


#pragma mark - inits

- (instancetype)init {
    if (self = [super init]) {
        self.modalPresentationStyle = UIModalPresentationOverCurrentContext;
        self.isDelectOptionRed = YES;
        self.redIndex = -1000;
    }
    return self;
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        self.modalPresentationStyle = UIModalPresentationOverCurrentContext;
        self.isDelectOptionRed = YES;
        self.redIndex = -1000;
    }
    return self;
}

#pragma mark - View Life Cycle

- (void)viewDidLoad {
    [super viewDidLoad];
    self.isHasTitle = !(!self.titleStr || [self.titleStr isEqualToString:@""]);
    
    if (self.isHasTitle) {
        self.titleLabel.text = self.titleStr;
        
    } else {
        self.customTitleView.hidden = YES;
        self.linerView.hidden = YES;
    }
    
    self.view.backgroundColor = [UIColor clearColor];
    if (self.optionsArray.count < 1) {
        self.tableViewHeightConstraint.constant = 0;
    } else {
        self.tableViewHeightConstraint.constant = self.optionsArray.count * 50 - 1;
    }
    [self.view layoutIfNeeded];
    CGRect bounds = self.view.bounds;
    bounds.origin.y -= self.optionsArray.count * 50 + 55 + (self.isHasTitle ? 60 : 0);
    self.view.bounds = bounds;
    
    self.tableView.separatorInset = UIEdgeInsetsZero;
    self.tableView.layoutMargins = UIEdgeInsetsZero;
    
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [UIView animateWithDuration:0.35 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
        self.view.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.35];
        CGRect bounds = self.view.bounds;
        bounds.origin.y = 0;
        self.view.bounds = bounds;

    } completion:nil];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self dismiss:nil];
}

#pragma mark - Action

- (IBAction)dismiss:(UIButton *)sender {
    
    __weak typeof(self) weakSelf = self;
    [UIView animateWithDuration:0.35 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
        self.view.backgroundColor = [UIColor clearColor];
        CGRect bounds = self.view.bounds;
        bounds.origin.y -= self.optionsArray.count * 50 + 55 + (self.isHasTitle ? 60 : 0);
        self.view.bounds = bounds;

    } completion:^(BOOL finished) {
        [self dismissViewControllerAnimated:NO completion:^{
            if (weakSelf.cancelBlock) {
                weakSelf.cancelBlock();
            }
        }];
    }];
}


#pragma mark - Table View Delegate & Data Source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.optionsArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellResuedId];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellResuedId];
        cell.layoutMargins = UIEdgeInsetsZero;
    }
    cell.textLabel.textAlignment = NSTextAlignmentCenter;
    cell.textLabel.text = self.optionsArray[indexPath.row];
    if ((self.isDelectOptionRed && [cell.textLabel.text isEqualToString:@"删除"]) || self.redIndex == indexPath.row) {
        cell.textLabel.textColor = [UIColor redColor];
    } else {
        cell.textLabel.textColor = [UIColor blackColor];
    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [UIView animateWithDuration:0.2 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
        self.view.backgroundColor = [UIColor clearColor];
        CGRect bounds = self.view.bounds;
        bounds.origin.y -= self.optionsArray.count * 50 + 55 + (self.isHasTitle ? 60 : 0);
        self.view.bounds = bounds;
        [self dismissViewControllerAnimated:NO completion:nil];
    } completion:^(BOOL finished) {
        if (self.didSelectOptionHandler) {
            self.didSelectOptionHandler(indexPath.row);
        }
        //协议中的可选方法需要做responds SEL判断
        if ([self.delegate respondsToSelector:@selector(optionSheetViewController:didSelectOptionAtIndex:)]) {
            [self.delegate optionSheetViewController:self didSelectOptionAtIndex:indexPath.row];
        }
        
    }]; 
}


@end
